package com.hyg.lib_base.MyView.SDCardImageSelect;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.lib_base.R;
import com.hyg.lib_base.mainUtils.Utils;

import java.util.ArrayList;


/* 选择手机图片 的列表适配器*/
public class CheckImageAdapter extends RecyclerView.Adapter<CheckImageAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Image> dataList;
    private OnItemClickLitener mOnItemClickLitener;
    private int mMaxCount;
    private boolean isSingle;

    public CheckImageAdapter(Context activity, ArrayList<Image> dataList) {
        this.context = activity.getApplicationContext();
        this.dataList = dataList;
    }

    public CheckImageAdapter(Context activity, ArrayList<Image> dataList, int maxCount, boolean isSingle) {
        this.context = activity.getApplicationContext();
        this.dataList = dataList;
        mMaxCount = maxCount;
        this.isSingle = isSingle;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_check_image,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.bindView(dataList.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (dataList == null) {
            return 0;
        }
        return dataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView CareImage;

        public ViewHolder(View itemView) {
            super(itemView);
            CareImage = itemView.findViewById(R.id.CareImage);
        }

        public void bindView(final Image image, final int pos) {
            if (pos == 0) {
                Glide.with(context)
                        .load(R.mipmap.camera)
//                        .crossFade(500)
                        .into(CareImage);
            } else {
                if (!Utils.isEmpty(image)) {
                    Glide.with(context)
                            .load(image.getPath())
//                            .crossFade(500)
                            .into(CareImage);
                }
            }
            // 如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = getLayoutPosition();
                        mOnItemClickLitener.onItemClick(itemView, pos);
                        //                    Toast.makeText(context,""+pos,Toast.LENGTH_SHORT).show();
                    }
                });

                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(itemView, pos);

                        return false;
                    }
                });
            }

        }

    }
}